package br.sed.theenemy.business.utils;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 * @author JulioCB
 * @email jcbjulio@gmail.com
 *
 */
public class Utils {

	public static boolean isNullOrEmpty(String string){
		if(string == null || string.isEmpty()){
			return true;
		}else{
			return false;
		}
	}
	
	public static boolean isNullOrEmptyOrNotNumber(String string){
		if(!isNullOrEmpty(string)){
			Pattern pattern = Pattern.compile("[^0-9]");
			Matcher myString = pattern.matcher(string);
			if(!myString.find()){
				return false;
			}
		}
		return true;
	}
	
	public static boolean isNullOrEmptyOrNotLetter(String string){
		if(!isNullOrEmpty(string)){
			//Pattern pattern = Pattern.compile("[a-zA-Z]");
			Pattern pattern = Pattern.compile("[a-zA-ZçÇáÁéÉíÍóÓúÚâÂêÊîÎôÔûÛãÃẽẼĩĨõÕũŨàÀèÈìÌòÒùÙ]");
			Matcher myString = pattern.matcher(string);
			if(!myString.find()){
				return false;
			}
		}
		return true;
	}
	
	public static Integer getStartFromPageMax(Integer page, Integer max){
		Integer start = 0;
		if (page!=1){
			start = (page-1)*max;
		}
		return start;
	}
	
	public static String MD5(String text){
		try {
			MessageDigest md;
			md = MessageDigest.getInstance("MD5");
			byte[] md5hash = new byte[32];
			md.update(text.getBytes("iso-8859-1"), 0, text.length());
			md5hash = md.digest();		
			//return convertToHex(md5hash);
			return String.format("%032x", new BigInteger(1, md5hash));
		} catch (NoSuchAlgorithmException e) {
			return null;
		} catch (UnsupportedEncodingException e) {
			return null;
		}
	}
}
